package com.doubao.protocol;

import com.alibaba.fastjson.JSON;
import com.doubao.config.AppConstant;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * description:Msg
 * <p>
 * author:sult
 * time: 2018/11/1 15:35
 **/
@Data
public class Msg implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(Msg.class);

    private Integer toType;
    private String from;
    private String to;
    /**
     * 消息类型：
     *
     */
    private Integer contentType;


    private String messageId;

    private Long ct;


    public static Msg handelMsg(String requestMsg) {
        Msg topMsg = null;
        try {
            if (null != requestMsg) {
                topMsg=JSON.parseObject(requestMsg, Msg.class);
                switch (topMsg.getContentType()){
                    case AppConstant.MSG_TYPE_MSG:
                        topMsg=JSON.parseObject(requestMsg, TextMsg.class);
                        break;
                    case AppConstant.MSG_TYPE_STSTUS:
                        topMsg=JSON.parseObject(requestMsg, StatusMsg.class);
                        break;
                }
            }
        } catch (Exception e) {
            logger.error("msg转换异常！", e);
        }
        return topMsg;
    }

    public Long getCt() {
        if(ct==null){
            ct=System.currentTimeMillis();
        }
        return ct;
    }
}
